package com.work.libtest2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.work.libtest2.dto.ExtraWorkDto;
import com.work.libtest2.dto.RewardAndPunishmentDTO;
import com.work.libtest2.entity.Department;
import com.work.libtest2.entity.Employee;
import com.work.libtest2.entity.RewardAndPunishment;
import com.work.libtest2.mapper.DepartmentMapper;
import com.work.libtest2.mapper.EmployeeMapper;
import com.work.libtest2.mapper.RewardAndPunishmentMapper;
import com.work.libtest2.service.RewardAndPunishmentService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * ClassName: RewardAndPunishment
 * Package: com.work.libtest2.service.impl
 * Description:
 *
 * @Author
 * @Create 2024/6/10 15:42
 * @Version 1.0
 */
@Service
public class RewardAndPunishmentServiceImpl extends ServiceImpl<RewardAndPunishmentMapper, RewardAndPunishment> implements RewardAndPunishmentService {

    @Resource
    private RewardAndPunishmentMapper rewardAndPunishmentMapper;

    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private DepartmentMapper departmentMapper;
    @Override
    public IPage<RewardAndPunishment> getPage(Integer currenPage, Integer pageSize, RewardAndPunishmentDTO rewardAndPunishment) {
        LambdaQueryWrapper<RewardAndPunishment> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(rewardAndPunishment.getEmpName()),RewardAndPunishment::getEmpName,rewardAndPunishment.getEmpName());
        IPage page = new Page(currenPage,pageSize);
        rewardAndPunishmentMapper.selectPage(page,lqw);

        List<RewardAndPunishment> list = new ArrayList<>();
        IPage newPage = new Page();
        BeanUtils.copyProperties(page,newPage);
        list = page.getRecords();
        newPage.setRecords(list);

        for (int i = 0; i < list().size(); i++) {
                list.get(i).setReward("￥ "+list.get(i).getReward());
                list.get(i).setPunishment("￥ "+list.get(i).getPunishment());
        }

        //根据奖惩
        if (Objects.equals(rewardAndPunishment.getStatus(), "1")){
            List<RewardAndPunishment> newList = new ArrayList<>();
            for (int i = 0; i < list().size(); i++) {
                if (!Objects.equals(list.get(i).getReward(), "0") && Strings.isNotEmpty(list.get(i).getRewardReason())){
                    newList.add(list.get(i));
                }
            }
            list = newList;
            newPage.setRecords(list);
            newPage.setTotal(list.size());
        }else if (Objects.equals(rewardAndPunishment.getStatus(), "2")){
            List<RewardAndPunishment> newList = new ArrayList<>();
            for (int i = 0; i < list().size(); i++) {
                if (!Objects.equals(list.get(i).getPunishment(), "0") && Strings.isNotEmpty(list.get(i).getPunishmentReason())){
                    newList.add(list.get(i));
                }
            }
            list = newList;
            newPage.setRecords(list);
            newPage.setTotal(list.size());
        }

        //        对日期范围区间进行判断
        if (rewardAndPunishment.getStartDate()!=""&&rewardAndPunishment.getStartDate()!=null&& !Objects.equals(String.valueOf(rewardAndPunishment.getStartDate()), "null")){
            List<RewardAndPunishment> extraWorkDtos = new ArrayList<>();
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date date = dateFormat.parse(rewardAndPunishment.getStartDate());
                for (int i = 0; i < list.size(); i++) {
                    String ewDate = String.valueOf(list.get(i).getRandpDate());
                    Date ewDateStr = dateFormat.parse(ewDate);
                    if (date.before(ewDateStr)){
                        extraWorkDtos.add(list.get(i));
                    }
                }
                list = extraWorkDtos;
                newPage.setRecords(list);
                newPage.setTotal(list.size());
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }

        if (rewardAndPunishment.getEndDate()!=null&&rewardAndPunishment.getEndDate()!=""&& !Objects.equals(rewardAndPunishment.getEndDate(), "null")){
            List<RewardAndPunishment> extraWorkDtos = new ArrayList<>();
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date date = dateFormat.parse(rewardAndPunishment.getEndDate());

                for (int i = 0; i < list.size(); i++) {
                    String ewDate = String.valueOf(list.get(i).getRandpDate());
                    Date ewDateStr = dateFormat.parse(ewDate);
                    if (date.after(ewDateStr)){
                        extraWorkDtos.add(list.get(i));
                    }
                }
                list = extraWorkDtos;
                newPage.setRecords(list);
                newPage.setTotal(list.size());
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }

        return newPage;
    }

    @Override
    public int addReward(RewardAndPunishment rewardAndPunishment) {
        Employee employee = employeeMapper.selectById(rewardAndPunishment.getEmpId());
        Department department = departmentMapper.selectById(employee.getDeptId());
        rewardAndPunishment.setEmpName(employee.getEmpName());
        rewardAndPunishment.setDeptName(department.getDeptName());
        return rewardAndPunishmentMapper.insert(rewardAndPunishment);
    }

    @Override
    public int deleteById(Integer rapId) {
        return rewardAndPunishmentMapper.deleteById(rapId);
    }

    @Override
    public RewardAndPunishment getById(Integer rapId) {
        return rewardAndPunishmentMapper.selectById(rapId);
    }

    @Override
    public int saveById(RewardAndPunishment rewardAndPunishment) {
        return rewardAndPunishmentMapper.updateById(rewardAndPunishment);
    }
}
